package com.roseski.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.roseski.dao.RoseskiInvoiceMapper;
import com.roseski.dto.InvoiceReq;
import com.roseski.dto.ServiceResult;
import com.roseski.enums.InvoiceStatusEnum;
import com.roseski.model.RoseskiInvoice;
import com.roseski.service.RoseskiInvoiceService;
import com.roseski.util.ErrorCodeEnum;
import com.roseski.util.MoneyUtil;

@RestController
@RequestMapping(value = "order/invoce")
public class RoseskiInvoiceController {
	
	@Autowired
	RoseskiInvoiceService roseskiInvoiceService;
	@Autowired
	RoseskiInvoiceMapper roseskiInvoiceMapper;
	
	@RequestMapping(value="applyInvoice")
	@ResponseBody
	public ServiceResult<String> applyInvoice(@RequestBody InvoiceReq req){
		ServiceResult<String> ret = new ServiceResult<>();
		ret.setCode(ErrorCodeEnum.SUCCESS.getCode());
		ret.setMessage(ErrorCodeEnum.SUCCESS.getMsg());
		RoseskiInvoice inv = roseskiInvoiceMapper.selectByOrderNo(req.getOrderNumber());
		if(inv != null) {
			ret.setCode(ErrorCodeEnum.INVOICE_EXISTS.getCode());
			ret.setMessage(ErrorCodeEnum.INVOICE_EXISTS.getMsg());
			return ret;
		}
		RoseskiInvoice invocice = new RoseskiInvoice();
		invocice.setOrderNo(req.getOrderNumber());
		invocice.setPhoneNumer(req.getPhoneNumer());
		invocice.setEin(req.getEin());
		invocice.setType(req.getType());
		invocice.setEmail(req.getEmail());
		invocice.setUserName(req.getUserName());
		invocice.setLookUpName(req.getLookUpName());
		invocice.setCreateTime(new Date());
		invocice.setTotalPrice(MoneyUtil.convertLongYuanToFen(Double.valueOf(req.getTotalPrice())));
		invocice.setStatus(InvoiceStatusEnum.APPLY.name());
		roseskiInvoiceService.insert(invocice);
		
		return ret;
		
	}

}
